package com.aaa.doctor.service.impl;

import cn.hutool.core.date.DateUtil;
import com.aaa.doctor.dao.RegistrationDao;
import com.aaa.doctor.feign.UserFeign;
import com.aaa.doctor.service.RegistrationService;
import com.aaa.doctor.vo.RegistrationVo;
import com.aaa.entity.Patient;
import com.aaa.entity.Registration;
import com.aaa.entity.User;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @version 1.8.0_181
 * @Author ZXJ
 * @Date 2023/3/26 14:45
 * @注释
 */
@Service
public class RegistrationServiceImpl implements RegistrationService {
    @Resource
    private RegistrationDao registrationDao;

    @Override
    public Result<IPage<Registration>> listRegistration(Integer currentPage, Integer pageSize, RegistrationVo registrationVo) {
        IPage<Registration> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Registration> wrapper=new QueryWrapper<>();
        //部门
        if (Objects.nonNull(registrationVo.getDeptId())){
            wrapper.eq("dept_id",registrationVo.getDeptId());
        }
        //患者姓名
        if (StringUtils.hasText(registrationVo.getPatientName())){
            wrapper.like("patient_name",registrationVo.getPatientName());
        }
        //挂号类型
        if (StringUtils.hasText(registrationVo.getSchedulingType())){
            wrapper.eq("scheduling_type",registrationVo.getSchedulingType());
        }
        //挂号时段
        if(StringUtils.hasText(registrationVo.getSubsectionType())){
            wrapper.eq("subsection_type",registrationVo.getSubsectionType());
        }
        //挂号状态
        if (StringUtils.hasText(registrationVo.getRegistrationStatus())){
            wrapper.eq("registration_status",registrationVo.getRegistrationStatus());
        }
        //挂号时间

        if (StringUtils.hasText(registrationVo.getQueryDate())){
            //String time = DateUtil.format(registrationVo.getVisitDate(), "yyyy-MM-dd");
            wrapper.eq("visit_date",registrationVo.getQueryDate());
        }
        IPage<Registration> page1 = registrationDao.selectPage(page,wrapper);
        return new Result<>(200,"患者挂号信息查询成功",page1);
    }

    @Override
    //修改待就诊状态
    public Result<Integer> handleCollect(String registrationId) {

        Integer i = registrationDao.updScuuess(registrationId);
        return new Result<Integer>(200,"修改待就诊状态成功",i);
    }

    @Override
    public Result<Integer> handleInvalid(String registrationId) {
        Integer i = registrationDao.updZuoFei(registrationId);
        return new Result<Integer>(200,"修改待就诊状态成功",i);
    }

    @Override
    public Result<Integer> handleReturn(String registrationId) {
        Integer i = registrationDao.updTuiFei(registrationId);

        return new Result<Integer>(200,"修改待就诊状态成功",i);
    }

    @Override//查询待就诊列表
    public Result<List<Registration>> queryToBeSeenRegistration(String schedulingType) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JWTUtils.getInfo(request.getHeader("token")).get("username");
        Long userId = userFeign.getByUsername(phone).getUserId();

        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("scheduling_type",schedulingType);
        wrapper.eq("registration_status","1");
        List<Registration> registrations = registrationDao.selectList(wrapper);

        return new Result<>(200,"查询待就诊信息成功",registrations);
    }

    @Override
    public Result<List<Registration>> queryVisitingRegistration(String schedulingType) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JWTUtils.getInfo(request.getHeader("token")).get("username");
        Long userId = userFeign.getByUsername(phone).getUserId();

        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("scheduling_type",schedulingType);
        wrapper.eq("registration_status","2");
        List<Registration> registrations = registrationDao.selectList(wrapper);
        return new Result<>(200,"查询就诊中信息成功",registrations);
    }

    @Override
    public Result<List<Registration>> queryVisitCompletedRegistration(String schedulingType) {
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JWTUtils.getInfo(request.getHeader("token")).get("username");
        Long userId = userFeign.getByUsername(phone).getUserId();
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("scheduling_type",schedulingType);
        wrapper.eq("registration_status","3");
        List<Registration> registrations = registrationDao.selectList(wrapper);
        return new Result<>(200,"查询就诊完成信息成功",registrations);

    }


    @Resource
    private UserFeign userFeign;
    @Override//修改新开列表中接诊后修改为就诊中状态
    public Result<String> handleVisit(String registrationId) {
        Registration registration = new Registration();
        registration.setRegistrationId(registrationId);
        registration.setRegistrationStatus("2");

        int i = registrationDao.updateById(registration);

        return new Result<>(200,"修改待就诊状态成功",i>0?"成功":"失败");
    }

    @Override
    public Result<String> getRegId(String registrationId) {
//        QueryWrapper<Registration> wrapper=new QueryWrapper<>();
//        wrapper.eq("registration_id",registrationId);
        Registration registration = new Registration();
        //registration.setRegistrationId(registrationId);
        registration.setRegistrationStatus("1");
        LocalDateTime now = LocalDateTime.now();
        registration.setUpdateTime(now);
        Integer u = registrationDao.update(registration);
        return new Result<>(200,"收费成功",u>0?"成功":"失败");

    }

    @Override
    public Result<String> wcjz(String registrationId) {
        Registration registration = new Registration();
        registration.setRegistrationStatus("3");
        registration.setRegistrationId(registrationId);
        int i1 = registrationDao.updateById(registration);

        return new Result<>(200,"完成就诊修改状态成功",i1>0?"成功":"失败");
    }



    @Override
    public Integer getRagNumber(Long deptId) {

        Integer regNumber = registrationDao.getRegNumber(deptId);
        System.out.println("regNumb=============er = " + regNumber);
//                registrationDao.getRegNumber(deptId) + 1;

        return regNumber;

    }

//    @Override
//    public Result<Patient> getByRegId(String registrationId) {
//        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
//        if (StringUtils.hasText(registrationId)){
//            wrapper.eq("registration_id",registrationId);
//        }
//        List<Patient> patients = registrationDao.selectByRegId(wrapper);
//        return new Result<Patient>(200,"根据registrationId查询成功",patients);
//    }


}
